package com.example.myapplication2;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_dialog);
        findViewById(R.id.dialog_button).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.dialog_button:
                //构造弹窗对象
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                //设置标题
                builder.setTitle("弹窗标题");
                //设置消息内容
                builder.setMessage("弹窗消息内容");
                //设置确定执行对象
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //提示消息
                        Toast.makeText(AlertDialogActivity.this,"确定了",Toast.LENGTH_SHORT).show();
                    }
                });
                //设置取消执行对象
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //提示消息
                        Toast.makeText(AlertDialogActivity.this,"取消了",Toast.LENGTH_SHORT).show();
                    }
                });
                //创建,显示
                builder.create().show();
                break;
        }
    }
}